package com.prs.crm.dao.ca.work.impl;

import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.ca.work.ArticleInfoDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.ca.work.ArticleInfo;
import com.prs.crm.domain.ca.work.ArticleTypes;

public class ArticleInfoDaoImpl extends BaseDaoImpl<ArticleInfo> implements
		ArticleInfoDao {



	@SuppressWarnings("unchecked")
	@Override
	public ArticleInfo getArticleInfo(final int aiArticleTypeid) {
		return (ArticleInfo) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session.createCriteria(ArticleInfo.class);
				criteria.add(Restrictions
						.eq("aiArticleTypeid", aiArticleTypeid));
				List<ArticleInfo> article = criteria.list();
				if (article.size() == 0) {
					return null;
				} else {
					return article.get(0);
				}
			}
		});
	}

	@Override
	public void saveOrArticleInfo(ArticleInfo articleInfo) {
		this.getHibernateTemplate().saveOrUpdate(articleInfo);
	}

	
	@SuppressWarnings("unchecked")
	@Override
	public List<ArticleInfo> getAllArticleInfo() {
		return (List<ArticleInfo>) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session.createCriteria(ArticleInfo.class);
				return criteria.list();
			}
		});
	}
}
